# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
import sys
class Solution:
    preindex = 0
    inindex = 0
    class TreeNode:
        def __init__(self, x):
            self.val = x
            self.left = None
            self.right = None

    def buildTree(self, preorder, inorder):
        """
        :type preorder: List[int]
        :type inorder: List[int]
        :rtype: TreeNode
        """
        return self.helper(preorder, inorder, sys.maxsize)

    def helper(self, preorder, inorder, target):
        if self.preindex == len(preorder) or inorder[self.inindex] == target:
            return None
        now = self.TreeNode(preorder[self.preindex])
        self.preindex += 1
        now.left = self.helper(preorder, inorder, now.val)
        self.inindex += 1
        now.right = self.helper(preorder, inorder, target)
        return now
